'Handles all script function calls
' $HeadURL: http://gabbscript.googlecode.com/svn/trunk/Gabbscript/FunctionCaller.vb $
' $Author: brookjensen500@hotmail.com $
' $Revision: 102 $
' $Date: 2007-11-16 18:57:45 +0000 (Fri, 16 Nov 2007) $

Module FunctionCaller
    'Delegate for script functions
    Delegate Function ScriptFunction(ByVal args As ArgList, ByRef curScript As Script) As Boolean

    Class FunctionList
        'Holds all the functions
        Private funcList As Hashtable

        Public Sub New()
            funcList = New Hashtable()
        End Sub

        Public Sub RegisterFunction(ByVal funcName As String, ByVal func As ScriptFunction)
            'Add the values to the hash table
            funcList.Add(funcName.ToLower(), func)
        End Sub

        Public Function CallFunction(ByVal funcName As String, ByVal args As ArgList, ByRef curScript As Script) As Boolean
            'Check if it exists
            If funcList.ContainsKey(funcName) Then
                'Get the function
                Dim func As ScriptFunction = funcList.Item(funcName)

                'And run it
                Try
                    Return func.Invoke(args, curScript)
                Catch ex As InvalidCastException
                    Console.WriteLine("ERROR: Invalid argument in function " & funcName)
                    Console.WriteLine("Function skipped")
                End Try
            End If
            Return False
        End Function
    End Class
End Module
